package com.ks.app.ad.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import javax.annotation.Resource;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.ParentPackage;
import com.ks.app.ad.service.KsadService;
import com.ks.app.admin.action.BaseAction;
import com.ks.app.admin.service.KsVersionService;


@ParentPackage("struts-default")
@InterceptorRef("defaultStack")
public class KsDownLoadAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	
	@Resource(name="ksadService")
	private KsadService ks;
	
	@Resource(name="ksVersionService")
	private KsVersionService kvs;
	
	
	/**
	 * 广告下载
	 * @return
	 */
	@Action(value="advdown")
	public String advdownload(){
		
		String isdown =request.getParameter("isdown")==null?"0":request.getParameter("isdown").toString().trim();
		String src    =request.getParameter("src")==null?"320X480":request.getParameter("src").toString().trim();
		String loc_ver=request.getParameter("loc_ver")==null?"0":request.getParameter("loc_ver").toString().trim();
		String clnt   =request.getParameter("clnt")==null?"":request.getParameter("clnt").toString().trim();;
		String smsc   =request.getParameter("smsc")==null?"0100":request.getParameter("smsc").toString().trim();
		String imsi   =request.getParameter("imsi")==null?"0":request.getParameter("imsi").toString().trim();

		String path0=ServletActionContext.getServletContext().getRealPath("/");
		String path=ks.getzipfilepath(src,smsc,loc_ver,isdown,path0).trim();  
		
		try
		{
		    if(path.length()>0)
		    { 
		   		 String fname=path.substring(path.lastIndexOf("\\")+1);
		    	 File file=new File(path); 
			     if(file.exists())
			     {
					   InputStream  ins=new FileInputStream(path); 
			     	   BufferedInputStream bins=new BufferedInputStream(ins);     //放到缓冲流里面 
			           OutputStream outs=response.getOutputStream();			  //获取文件输出IO流 
			           BufferedOutputStream bouts=new BufferedOutputStream(outs); 
			           response.setContentType("application/x-download");		                //设置response内容的类型 
			           response.setHeader("Content-disposition","attachment;filename="+ fname); //设置头部信息 
			           int bytesRead = 0; 
			           byte[] buffer = new byte[8192]; 
			           //开始向网络传输文件流 
			           while ((bytesRead = bins.read(buffer, 0, 8192)) != -1) { 
			               bouts.write(buffer, 0, bytesRead); 
			           } 
			           bouts.flush();//这里一定要调用flush()方法 
			           ins.close(); 
			           bins.close(); 
			           outs.close(); 
			           bouts.close(); 			
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	/**
	 * 客户端apk下载
	 * @return
	 */
	@Action(value="apkdown")
	public String apkdown(){
		
		String isdown=request.getParameter("isdown")==null?"0":request.getParameter("isdown").toString().trim();
		String src=request.getParameter("src")==null?"320X480":request.getParameter("src").toString().trim();
		String loc_ver=request.getParameter("ver")==null?"0":request.getParameter("ver").toString().trim();
		String clnt=request.getParameter("clnt")==null?"":request.getParameter("clnt").toString().trim();;
		String smsc=request.getParameter("smsc")==null?"0100":request.getParameter("smsc").toString().trim();
		String imsi=request.getParameter("imsi")==null?"0":request.getParameter("imsi").toString().trim();
		String path=kvs.getApkfilepath(loc_ver,isdown).trim();  
		
		try
		{
			
		    if(path.length()>0)
		    { 
		   		 String fname=path.substring(path.lastIndexOf("\\")+1);
		    	 File file=new File(path); 
			     if(file.exists())
			     {
			     	   fname=URLEncoder.encode(fname,"utf-8");
					   InputStream  ins=new FileInputStream(path); 
			     	   BufferedInputStream bins=new BufferedInputStream(ins);//放到缓冲流里面 
			           OutputStream outs=response.getOutputStream();//获取文件输出IO流 
			           BufferedOutputStream bouts=new BufferedOutputStream(outs); 
			           response.setContentType("application/x-download");//设置response内容的类型 
			           response.setHeader("Content-disposition","attachment;filename="+ fname);//设置头部信息 
			           int bytesRead = 0; 
			           byte[] buffer = new byte[8192]; 
			           //开始向网络传输文件流 
			           while ((bytesRead = bins.read(buffer, 0, 8192)) != -1) { 
			               bouts.write(buffer, 0, bytesRead); 
			           } 
			           bouts.flush();//这里一定要调用flush()方法 
			           ins.close(); 
			           bins.close(); 
			           outs.close(); 
			           bouts.close(); 			
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	
		return null;
	}

}
